#include <windows.h>
#include "common.h"

struct params PARAMS;

const char g_szClassName[] = "myWindowClass";


void broadcast(HWND hWnd)
{
	COPYDATASTRUCT cds;

	cds.dwData = 8134;
	cds.cbData = sizeof(PARAMS);
	cds.lpData = &PARAMS;

	SendMessage(HWND_BROADCAST,
			WM_COPYDATA,
			(WPARAM)hWnd,
			(LPARAM)&cds);
}

int GetSelectedRadioButton(HWND hWnd, int firstId, int lastId)
{
	int id;
	for (id = firstId; id <= lastId; ++id)
	{
		HWND hBtn = GetDlgItem(hWnd, id);		/* returns a handle to a control in the
												   hWnd dialog box */
		LRESULT status = SendMessage(hBtn, BM_GETCHECK, 0, 0);
		if (status == BST_CHECKED)
			return id;
	}
	return -1;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	int wmId;
	switch(msg)
	{
		case WM_COMMAND:
			wmId = LOWORD(wParam);
			if (10001 <= wmId && wmId <= 10003)
			{
				CheckRadioButton(hWnd, 10001, 10003, wmId);
				PARAMS.colorid = GetSelectedRadioButton(hWnd, 10001, 10003);
				broadcast(hWnd);
			}
			if (10004 <= wmId && wmId <= 10007)
			{
				CheckRadioButton(hWnd, 10004, 10007, wmId);
				PARAMS.shapeid = GetSelectedRadioButton(hWnd, 10004, 10007);
				broadcast(hWnd);
			}
			if (wmId == 10008)
			{
				HWND hCbx = GetDlgItem(hWnd, 10008);
				LRESULT checked = SendMessage(hCbx, BM_GETCHECK, 0, 0);
				SendMessage(hCbx,
						BM_SETCHECK,
						checked == BST_CHECKED ? BST_UNCHECKED : BST_CHECKED,
						0);
				PARAMS.draw = ! PARAMS.draw;
				broadcast(hWnd);
			}
			break;
		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
		LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hWnd;
	MSG Msg;

	wc.cbSize        = sizeof(WNDCLASSEX);
	wc.style         = 0;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hWnd = CreateWindowEx(
			WS_EX_CLIENTEDGE,
			g_szClassName,
			"Parameters",
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT, CW_USEDEFAULT, 225, 220,
			NULL, NULL, hInstance, NULL);

	if(hWnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
				MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	CreateWindow("button", "Red",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_GROUP,
			10, 10, 80, 40, hWnd, (HMENU)10001, hInstance, NULL);
	CreateWindow("button", "Green",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			10, 50, 80, 40, hWnd, (HMENU)10002, hInstance, NULL);
	CreateWindow("button", "Blue",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			10, 90, 80, 40, hWnd, (HMENU)10003, hInstance, NULL);
	CheckRadioButton(hWnd, 10001, 10003, 10001);
	PARAMS.colorid = 10001;

	CreateWindow("button", "Rhomb",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON | WS_GROUP,
			110, 10, 80, 30, hWnd, (HMENU)10004, hInstance, NULL);
	CreateWindow("button", "Square",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			110, 40, 80, 30, hWnd, (HMENU)10005, hInstance, NULL);
	CreateWindow("button", "Circle",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			110, 70, 80, 30, hWnd, (HMENU)10006, hInstance, NULL);
	CreateWindow("button", "Star",
			WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,
			110, 100, 80, 30, hWnd, (HMENU)10007, hInstance, NULL);
	CheckRadioButton(hWnd, 10004, 10007, 10004);
	PARAMS.shapeid = 10004;

	CreateWindow("button", "Draw",
			WS_CHILD | WS_VISIBLE | BS_CHECKBOX,
			10, 150, 180, 30, hWnd, (HMENU)10008, hInstance, NULL);
	PARAMS.draw = 0;

	broadcast(hWnd);

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
